package com.example.myapplication2;

import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication2.Receiver.StandardReceiver;

public class BroadcastStandardActivity extends AppCompatActivity implements View.OnClickListener {

    private StandardReceiver standardReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_broadcast_standard);
        findViewById(R.id.sendBroadcastStandard).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.sendBroadcastStandard:
                System.out.println("发送...");
                //发送广播
                sendBroadcast(new Intent("com.example.myapplication2"));
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        //创建广播接收者
        standardReceiver = new StandardReceiver();
        //创建意图过滤器
        IntentFilter intentFilter = new IntentFilter("com.example.myapplication2");
        //注册接收者和意图过滤器
        registerReceiver(standardReceiver, intentFilter);
    }

    @Override
    protected void onStop() {
        super.onStop();
        //注销广播
        unregisterReceiver(standardReceiver);
    }
}